package com.cat.conf.mvc;

import com.google.common.collect.Lists;
// import com.jaws.core.common.log.Log;
// import com.jaws.core.configuration.CommonProperties;
// import com.jaws.core.configuration.CommonProperties.LogSens;
// import lombok.extern.slf4j.Slf4j;
// import org.apache.commons.collections.CollectionUtils;
// import org.apache.commons.lang.ArrayUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.List;

//import org.assertj.core.util.Lists;

/**
 * controller请求拦截
 *
 * @date 2018-02-28 16:27:48
 * @since v1.0.0
 */
// @Slf4j
@Aspect
public class ReqRspAspect {

	/**
	 * 切入点
	 */
	@Pointcut("execution(public * com..*.controller..*.*(..))")
	public void pointCut() {
	}

	// @Resource
	// private CommonProperties commonProperties;

	/**
	 * 拦截要执行的目标方法
	 */
	@Around("pointCut()")
	public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
		long startTimestamp = System.currentTimeMillis();
		// 非接口不拦截
		MethodSignature ms = (MethodSignature) pjp.getSignature();
		Method method = ms.getMethod();
		if (AnnotationUtils.findAnnotation(method, RequestMapping.class) == null) {
			return pjp.proceed();
		}
		RequestAttributes ra = RequestContextHolder.getRequestAttributes();
		ServletRequestAttributes sra = (ServletRequestAttributes) ra;
		HttpServletRequest request = sra.getRequest();
		Object[] arguments = pjp.getArgs();
		// log.info(Log.op("ReqRspAspect.aroundAdvice").msg("【HTTP请求{}】{} ").toString(), Thread.currentThread().getId(), request.getMethod());
		// if (ArrayUtils.isNotEmpty(arguments)) {
		// 	log.info(Log.op("【HTTP{" + Thread.currentThread().getId() + "}】 ").kv("arguments", getArguments(arguments)).toString());
		// }
		// // result的值就是被拦截方法的返回值
		// Object result = null;
		// try {
		// 	result = pjp.proceed();
		// } finally {
		// 	long endTimestamp = System.currentTimeMillis();
		// 	long takeTime = endTimestamp - startTimestamp;
		// 	log.info(Log.op("ReqRspAspect.aroundAdvice").msg("【HTTP响应{}】{} , 耗时 : {} ms ").kv("result", result).toString(), Thread.currentThread().getId(), request.getMethod(), takeTime);
		// }
		// return result;
		return pjp.proceed();
	}

	// private List<Object> getArguments(Object[] arguments) {
	// 	List<Object> args = Lists.newArrayList();
	// 	List<LogSens> logSens = commonProperties.getLogSens();
	// 	for (Object arg : arguments) {
	// 		if (!isExclude(arg, logSens)) {
	// 			args.add(arg);
	// 		}
	// 	}
	// 	return args;
	// }
    //
	// private boolean isExclude(Object arg, List<LogSens> logSens) {
	// 	if (HttpServletRequest.class.isAssignableFrom(arg.getClass())) {
	// 		return true;
	// 	}
	// 	if (HttpServletResponse.class.isAssignableFrom(arg.getClass())) {
	// 		return true;
	// 	}
	// 	if(MultipartFile.class.isAssignableFrom(arg.getClass())){
	// 		return true;
	// 	}
	// 	if (CollectionUtils.isNotEmpty(logSens)) {
	// 		for (LogSens exlude : logSens) {
	// 			if (exlude.getExcludeClass().isAssignableFrom(arg.getClass())) {
	// 				return true;
	// 			}
	// 		}
	// 	}
    //
	// 	return false;
	// }
}
